import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/home/index'
import Login from '@/views/login/index'
import Register from '@/views/login/register'
import Mine from '@/views/mine'
import Order from '@/views/order'
import Detail from '@/views/login/detail'
import CarDetail from '@/views/carDetail'
import Part from '@/views/part'
import Driving from '@/views/order/driving'
import OrderRules from '@/views/carDetail/orderRules'
import Rules from '@/views/home/rules'
import PartDetail from '@/views/home/partDetail'
import Advertisement from '@/views/home/advertisement'
import Card from '@/views/mine/card'
import BuyCard from '@/views/home/buyCard'
import AllOrders from '@/views/mine/allOrders'
import MyOrder from '@/views/mine/myOrder'
import ToPay from '@/views/mine/toPay'
import Pay from '@/views/carDetail/pay'
import store from '@/store/index'
import { Notify } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    path: '',
    redirect: '/home',
    component: Home
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/mine',
    name: 'Mine',
    component: Mine
  },
  {
    path: '/order',
    name: 'Order',
    component: Order
  },
  {
    path: '/detail',
    name: 'Detail',
    component: Detail
  },
  {
    path: '/Cardetail',
    name: 'CarDetail',
    component: CarDetail
  },
  {
    path: '/part',
    name: 'Part',
    component: Part
  },
  {
    path: '/driving',
    name: 'Driving',
    component: Driving
  },
  {
    path: '/orderRules',
    name: 'Driving',
    component: OrderRules
  },
  {
    path: '/rules',
    name: 'Rules',
    component: Rules
  },
  {
    path: '/partDetail',
    name: 'PartDetail',
    component: PartDetail
  },
  {
    path: '/advertisement',
    name: 'Advertisement',
    component: Advertisement
  },
  {
    path: '/myCard',
    name: 'Card',
    component: Card
  },
  {
    path: '/buyCard',
    name: 'BuyCard',
    component: BuyCard
  },
  {
    path: '/allOrders',
    name: 'AllOrders',
    component: AllOrders
  },
  {
    path: '/myOrder',
    name: 'MyOrder',
    component: MyOrder
  },
  {
    path: '/toPay',
    name: 'ToPay',
    component: ToPay
  },
  {
    path: '/pay',
    name: 'Pay',
    component: Pay
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})
router.beforeEach((to, from, next) => {
  const isLogin = store.state.isLogin

  if (to.path === '/mine' || to.path === '/order' || to.path === '/buyCard') {
    if (isLogin) {
      next()
    } else {
      Notify({ type: 'danger', message: '还未登陆，请先完成登录' })
      router.push('/login')
    }
  } else {
    next()
  }
})

export default router
